<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 
 * 许愿树
 * @author viviqin
 *
 */

class Tree_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
    function get_list($data){
    	$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		$_order = " order by id desc ";
		$sql = "select t.id,u.username,t.content,t.user_id,t.date,t.status from rsh_tree as t left join rsh_user as u on t.user_id=u.user_id";
		
		$where = ' where 1=1 ';
		if(isset($data['status'])){
			$where .=' and t.status= '.$data['status'];
		}
		$sql .= $where;
		$sql .= $_order;
    	if(isset($data['limit']) && $data['limit'] == 'all'){
			$query = $this->db->query($sql);
			$data['list'] = $query->result_array();
			return $data;
		}else{
			$sql .= $_limit;
			$query = $this->db->query($sql);
			$data['list'] = $query->result_array();
		}
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_tree as t 
						left join rsh_user as u on t.user_id=u.user_id
						";
		
		$query = $this->db->query($sql_count);
		$data['count'] = $query->row()->count;
		
		return $data;
		
    }
    
    function get_by_userid($userid){
    	$sql = "select * from rsh_tree where user_id=".$userid;
		$query = $this->db->query($sql);
		$data = $query->result_array();
		$_uid = array();
		if($data){
			foreach ($data as $v){
				$_uid[] = $v['user_id'] ;
			}		
		}
		return $_uid;
    }
    
    function add($data){
    	$this->db->insert('rsh_tree', $data);
		return $this->db->insert_id();
    }
    
	function update($id, $data){
		$this->db->where('id', $id);
		$this->db->update('rsh_tree', $data);
		return $this->db->affected_rows();
	}
	
	
	function add_nianhui($data){
    	$this->db->insert('rsh_nianhui', $data);
		return $this->db->insert_id();
    }
	
    function get_nianhui_list($data){
    	$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		$_order = " order by id desc ";
		$sql = "select t.id,u.username,t.content,t.user_id,t.date,t.status,t.superb from rsh_nianhui as t left join rsh_user as u on t.user_id=u.user_id";
		
		$where = ' where 1=1 ';
		if(isset($data['status'])){
			$where .=' and t.status= '.$data['status'];
		}
		$sql .= $where;
		$sql .= $_order;
    	if(isset($data['limit']) && $data['limit'] == 'all'){
			$query = $this->db->query($sql);
			$data['list'] = $query->result_array();
			return $data;
		}else{
			$sql .= $_limit;
			$query = $this->db->query($sql);
			$data['list'] = $query->result_array();
		}
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_nianhui as t 
						left join rsh_user as u on t.user_id=u.user_id
						";
		
		$query = $this->db->query($sql_count);
		$data['count'] = $query->row()->count;
		
		return $data;
    }
    
	function update_nianhui($id, $data){
		$this->db->where('id', $id);
		$this->db->update('rsh_nianhui', $data);
		return $this->db->affected_rows();
	}
	
	function get_nianhui_by_id($id){
		$this->db->where('id', $id);
		$query = $this->db->get('rsh_nianhui');
		return $query->row_array();
	}
	
	function get_reg_list($data){
    	$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		$_order = " order by id desc ";
		$sql = "select t.id,u.username,t.content,t.user_id,t.date,t.status from rsh_reg100 as t left join rsh_user as u on t.user_id=u.user_id";
		
		$where = ' where 1=1 ';
		if(isset($data['status'])){
			$where .=' and t.status= '.$data['status'];
		}
		$sql .= $where;
		$sql .= $_order;
    	if(isset($data['limit']) && $data['limit'] == 'all'){
			$query = $this->db->query($sql);
			$data['list'] = $query->result_array();
			return $data;
		}else{
			$sql .= $_limit;
			$query = $this->db->query($sql);
			$data['list'] = $query->result_array();
		}
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_reg100 as t 
						left join rsh_user as u on t.user_id=u.user_id
						";
		
		$query = $this->db->query($sql_count);
		$data['count'] = $query->row()->count;
		
		return $data;
		
    }
    
    
    
    function add_reg($data){
    	$this->db->insert('rsh_reg100', $data);
		return $this->db->insert_id();
    }
    
	function update_reg($id, $data){
		$this->db->where('id', $id);
		$this->db->update('rsh_reg100', $data);
		return $this->db->affected_rows();
	}
	
	function get_reg_by_userid($userid){
    	$sql = "select * from rsh_reg100 where user_id=".$userid;
		$query = $this->db->query($sql);
		$data = $query->result_array();
		$_uid = array();
		if($data){
			foreach ($data as $v){
				$_uid[] = $v['user_id'] ;
			}		
		}
		return $_uid;
    }
    
    
    
	function get_activity_list($data){
    	$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		$_order = " order by id desc ";
		$sql = "select t.id,u.username,t.type,t.content,t.user_id,t.date,t.status from rsh_activity as t left join rsh_user as u on t.user_id=u.user_id";
		
		$where = ' where 1=1 ';
		if(isset($data['status'])){
			$where .=' and t.status= '.$data['status'];
		}
		
		if(isset($data['type'])){
			$where .=' and t.type= '.$data['type'];
		}
		
		$sql .= $where;
		$sql .= $_order;
    	if(isset($data['limit']) && $data['limit'] == 'all'){
			$query = $this->db->query($sql);
			$data['list'] = $query->result_array();
			return $data;
		}else{
			$sql .= $_limit;
			$query = $this->db->query($sql);
			$data['list'] = $query->result_array();
		}
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_activity as t 
						left join rsh_user as u on t.user_id=u.user_id
						";
		$sql_count .= $where;
		
		$query = $this->db->query($sql_count);
		$data['count'] = $query->row()->count;
		
		return $data;
		
    }
    
    
    
    function add_activity($data){
    	$this->db->insert('rsh_activity', $data);
		return $this->db->insert_id();
    }
    
	function update_activity($id, $data){
		$this->db->where('id', $id);
		$this->db->update('rsh_activity', $data);
		return $this->db->affected_rows();
	}
	
	function get_activity_by_userid($userid){
    	$sql = "select * from rsh_activity where user_id=".$userid;
		$query = $this->db->query($sql);
		$data = $query->result_array();
		$_uid = array();
		if($data){
			foreach ($data as $v){
				$_uid[] = $v['user_id'] ;
			}		
		}
		return $_uid;
    }
    
	function add_activity_comment($data){
    	$this->db->insert('rsh_activity_comment', $data);
		return $this->db->insert_id();
    }
    
    //根据活动ID统计8种不同类型的评论
    function count_activity_comment($activity_id){
    	//1.超赞 2.送花 3.开心 4.扯淡 5.激动 6.蛋疼 7.愤怒 8.伤心
    	
    	$sql_count1 = "select count(*) as count from rsh_activity_comment where type_id = 1 and activity_id=".$activity_id;
		$query1 = $this->db->query($sql_count1);
		$data['best_count'] = $query1->row()->count;
		
		$sql_count2 = "select count(*) as count from rsh_activity_comment where type_id = 2 and activity_id=".$activity_id;
		$query2 = $this->db->query($sql_count2);
		$data['flower_count'] = $query2->row()->count;
		
		$sql_count3 = "select count(*) as count from rsh_activity_comment where type_id = 3 and activity_id=".$activity_id;
		$query3 = $this->db->query($sql_count3);
		$data['good_count'] = $query3->row()->count;
		
		$sql_count4 = "select count(*) as count from rsh_activity_comment where type_id =4 and activity_id=".$activity_id;
		$query4 = $this->db->query($sql_count4);
		$data['chedan_count'] = $query4->row()->count;
		
		$sql_count5 = "select count(*) as count from rsh_activity_comment where type_id = 5 and activity_id=".$activity_id;
		$query5 = $this->db->query($sql_count5);
		$data['jidong_count'] = $query5->row()->count;
		
		$sql_count6 = "select count(*) as count from rsh_activity_comment where type_id = 6 and activity_id=".$activity_id;
		$query6 = $this->db->query($sql_count6);
		$data['danteng_count'] = $query6->row()->count;
		
		$sql_count7 = "select count(*) as count from rsh_activity_comment where type_id = 7 and activity_id=".$activity_id;
		$query7 = $this->db->query($sql_count7);
		$data['angry_count'] = $query7->row()->count;
		
		$sql_count8 = "select count(*) as count from rsh_activity_comment where type_id = 8 and activity_id=".$activity_id;
		$query8 = $this->db->query($sql_count8);
		$data['sad_count'] = $query8->row()->count;
		
		return $data;
    }
}